package com.talent.core.protocol.mqtt;

import com.talent.core.protocol.mqtt.codec.TlMqttMessageCodec;
import com.talent.core.protocol.mqtt.codec.encoder.*;
import com.talent.core.protocol.mqtt.handler.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName：TlMqttChannelInitializer
 * @Author: hszhou
 * @Date: 2024/12/9 16:14
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
public class TlMqttChannelInitializer extends ChannelInitializer {

    private static final Logger logger = LoggerFactory.getLogger(TlMqttChannelInitializer.class);


    private TlMqttMessageCodec mqttMessageCodec;

    private TlMqttConnectHandler mqttConnectHandler;

    private TlMqttPublishHandler mqttPublishHandler;

    private TlMqttSubScribeHandler mqttSubScribeHandler;

    private TlMqttUnSubScribeHandler mqttUnSubScribeHandler;

    private TlMqttHeartBeatHandler mqttHeartBeatHandler;

    private TlMqttDisconnectHandler mqttDisconnectHandler;

    private TlMqttPubCompHandler mqttPubCompHandler;

    private TlMqttPubAckHandler mqttPubAckHandler;

    private TlMqttPubRecHandler mqttPubRecHandler;

    private TlMqttPubRelHandler mqttPubRelHandler;

    private TlMqttConnackEncoder mqttConnackEncoder;

    private TlMqttHeaderBeatEncoder mqttHeaderBeatEncoder;

    private TlMqttSubAckEncoder mqttSubAckEncoder;

    private TlMqttUnSubAckEncoder mqttUnSubAckEncoder;

    private TlMqttPublishEncoder mqttPublishEncoder;


    private TlMqttPubAckEncoder mqttPubAckEncoder;

    private TlMqttPubRecEncoder mqttPubRecEncoder;

    private TlMqttPubCompEncoder mqttPubCompEncoder;

    private TlMqttExceptionHandler mqttExceptionHandler;

    private HeartbeatEventTriggered heartbeatEventTriggered;



    public TlMqttChannelInitializer(
            TlMqttMessageCodec mqttMessageCodec,
                                    TlMqttConnectHandler mqttConnectHandler,
                                    TlMqttPublishHandler mqttPublishHandler,
                                    TlMqttSubScribeHandler mqttSubScribeHandler,
                                    TlMqttUnSubScribeHandler mqttUnSubScribeHandler,
                                    TlMqttHeartBeatHandler mqttHeartBeatHandler,
                                    TlMqttDisconnectHandler mqttDisconnectHandler,
                                    TlMqttPubCompHandler mqttPubCompHandler,
                                    TlMqttPubAckHandler mqttPubAckHandler,
                                    TlMqttPubRecHandler mqttPubRecHandler,
                                    TlMqttPubRelHandler mqttPubRelHandler,
                                    TlMqttConnackEncoder connackEncoder,
                                    TlMqttHeaderBeatEncoder mqttHeaderBeatEncoder,
                                    TlMqttSubAckEncoder mqttSubAckEncoder,
                                    TlMqttUnSubAckEncoder mqttUnSubAckEncoder,
                                    TlMqttPublishEncoder mqttPublishEncoder,
                                    TlMqttPubAckEncoder mqttPubAckEncoder,
                                    TlMqttPubRecEncoder mqttPubRecEncoder,
                                    TlMqttPubCompEncoder mqttPubCompEncoder,
                                    TlMqttExceptionHandler mqttExceptionHandler,
                                    HeartbeatEventTriggered heartbeatEventTriggered
                                    ){

        this.mqttMessageCodec=mqttMessageCodec;
        this.mqttConnectHandler=mqttConnectHandler;
        this.mqttPublishHandler=mqttPublishHandler;
        this.mqttSubScribeHandler=mqttSubScribeHandler;
        this.mqttUnSubScribeHandler=mqttUnSubScribeHandler;
        this.mqttHeartBeatHandler=mqttHeartBeatHandler;
        this.mqttDisconnectHandler=mqttDisconnectHandler;
        this.mqttPubCompHandler=mqttPubCompHandler;
        this.mqttPubAckHandler=mqttPubAckHandler;
        this.mqttPubRecHandler=mqttPubRecHandler;
        this.mqttPubRelHandler=mqttPubRelHandler;
        this.mqttConnackEncoder=connackEncoder;
        this.mqttHeaderBeatEncoder=mqttHeaderBeatEncoder;
        this.mqttSubAckEncoder=mqttSubAckEncoder;
        this.mqttUnSubAckEncoder=mqttUnSubAckEncoder;
        this.mqttPublishEncoder=mqttPublishEncoder;
        this.mqttPubAckEncoder=mqttPubAckEncoder;
        this.mqttPubRecEncoder=mqttPubRecEncoder;
        this.mqttPubCompEncoder=mqttPubCompEncoder;
        this.mqttExceptionHandler=mqttExceptionHandler;
        this.heartbeatEventTriggered=heartbeatEventTriggered;

    }
    @Override
    protected void initChannel(Channel channel) throws Exception {

        ChannelPipeline pipeline = channel.pipeline();
        //协议解析
        pipeline.addLast(new TlMqttMessageCodec())
        //连接处理
        .addLast(mqttConnectHandler)
        //publish消息接收处理器
        .addLast(mqttPublishHandler)
        //订阅主题处理
        .addLast(mqttSubScribeHandler)
        //取消订阅处理
        .addLast(mqttUnSubScribeHandler)
        //心跳检测处理
        .addLast(mqttHeartBeatHandler)
        //断开连接处理
        .addLast(mqttDisconnectHandler)
        //pubcomp处理
        .addLast(mqttPubCompHandler)
        //puback处理
        .addLast(mqttPubAckHandler)
        //rec处理
        .addLast(mqttPubRecHandler)
        //rel处理
        .addLast(mqttPubRelHandler)
        //ack编码
        .addLast(mqttConnackEncoder)
        //心跳恢复编码
        .addLast(mqttHeaderBeatEncoder)
        //订阅ack编码
        .addLast(mqttSubAckEncoder)
        //取消订阅ack编码
        .addLast(mqttUnSubAckEncoder)
        //publish编码
        .addLast(mqttPublishEncoder)
        //puback编码
        .addLast(mqttPubAckEncoder)
        //订阅ack编码
        //rec编码
        .addLast(mqttPubRecEncoder)
        //comp编码
        .addLast(mqttPubCompEncoder)
        //异常处理
        .addLast(mqttExceptionHandler)
        //心跳检测
        .addLast(heartbeatEventTriggered);


//        //协议解析
//        pipeline.addLast(new TlMqttMessageCodec());
//        //连接处理
//        pipeline.addLast(new TlMqttConnectHandler(mqttStore));
//        //publish消息接收处理器
//        pipeline.addLast(new TlMqttPublishHandler(mqttStore));
//        //订阅主题处理
//        pipeline.addLast(new TlMqttSubScribeHandler(mqttStore));
//        //取消订阅处理
//        pipeline.addLast(new TlMqttUnSubScribeHandler(mqttStore));
//        //心跳检测处理
//        pipeline.addLast(new TlMqttHeartBeatHandler());
//        //断开连接处理
//        pipeline.addLast(new TlMqttDisconnectHandler(mqttStore));
//        //pubcomp处理
//        pipeline.addLast(new TlMqttPubCompHandler());
//        //puback处理
//        pipeline.addLast(new TlMqttPubAckHandler(mqttStore));
//        //rec处理
//        pipeline.addLast(new TlMqttPubRecHandler());
//        //rel处理
//        pipeline.addLast(new TlMqttPubRelHandler());
//        //ack编码
//        pipeline.addLast(new TlMqttConnackEncoder());
//        //心跳恢复编码
//        pipeline.addLast(new TlMqttHeaderBeatEncoder());
//
//        //订阅ack编码
//        pipeline.addLast(new TlMqttSubAckEncoder());
//        //取消订阅ack编码
//        pipeline.addLast(new TlMqttUnSubAckEncoder());
//        //publish编码
//        pipeline.addLast(new TlMqttPublishEncoder());
//        //puback编码
//        pipeline.addLast(new TlMqttPubAckEncoder());
//        //订阅ack编码
//        //rec编码
//        pipeline.addLast(new TlMqttPubRecEncoder());
//        //comp编码
//        pipeline.addLast(new TlMqttPubCompEncoder());
//        //异常处理
//        pipeline.addLast(new TlMqttExceptionHandler(mqttStore));
//        //心跳检测
//        pipeline.addLast(new HeartbeatEventTriggered());
    }
}
